<?php
/**
 * User: @lixu
 * Date: 2019-06-28
 * Time: 11:17
 */

ini_set('display_errors', 1);
error_reporting(-1);

require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\theParagraph\Store as mStore;
use Biz\model\trade\Order as mOrder;
use Biz\model\trade\OrderGoods as mOrderGoods;
use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\model\trade\OrderPackage as mOrderPackage;
use Biz\model\trade\OrderExtend as mOrderExtend;
use Biz\service\esSearch\Order as OrderSearch;
use Biz\model\trade\CustomizePayWay as mCustomizePayWay;

// order 信息
$orderField = ['id', 'order_no', 'status', 'goods_amount', 'is_commented', 'pay_amount', 'from_source', 'order_type', 'shop_id', 'uid', 'coupon_id', 's_coupon_id', 'pay_way', 'pay_no', 'consignee_phone', 'consignee_name', 'express_no', 'express_company', 'buy_num', 'addtime', 'pay_time', 'confirm_time', 'delivery_time','refunding_status'];

$start = 0;

do {

$orderList = mOrder::select($orderField, ['LIMIT' => [$start, 1000], 'ORDER' => ['addtime' => 'DESC']]);
$orderList = array_column($orderList, null,  'order_no');

// 获取订单号
$orderNos = array_keys($orderList);

// order_goods 信息
$orderGoodsField = ['title', 'goods_id', 'sku_id', 'category_id', 'second_category_id', 'order_no'];
$orderGoodsList = mOrderGoods::select($orderGoodsField, ['order_no' => $orderNos]);

// order_refund 信息
$orderRefundField = ['status(refund_status)', 'order_no', 'refund_order_no'];
$orderRefundList = mOrderRefund::select($orderRefundField, ['order_no' => $orderNos]);

// order_package 信息
$orderPackageField = ['order_no', 'express_no', 'express_company'];
$orderPackageList = mOrderPackage::select($orderPackageField, ['order_no' => $orderNos]);

// order_extend 信息
$orderExtendField = ['order_no', 'store_id'];
$orderExtendList = mOrderExtend::select($orderExtendField, ['order_no' => $orderNos]);

// 自定义支付信息
$customizePayWayField = ['order_no', 'pay_type'];
$customizePayWayList = mCustomizePayWay::select($customizePayWayField, ['order_no' => $orderNos]);

$sum = count($orderList);
foreach ($orderList as $orderNo => $order) {
    $orderList[$orderNo]['order_goods'] = [];
    $orderList[$orderNo]['order_refund'] = [];
    $orderList[$orderNo]['order_package'] = [];
    $orderList[$orderNo]['order_extend'] = [];

    foreach ($orderGoodsList as $orderGoods) {
        if($orderNo == $orderGoods['order_no']) {
            unset($orderGoods['order_no']);
            $orderList[$orderNo]['order_goods'][] = $orderGoods;
        }
    }

    foreach ($orderRefundList as $orderRefund) {
        if($orderNo == $orderRefund['order_no']) {
            unset($orderRefund['order_no']);
            $orderList[$orderNo]['order_refund'][] = $orderRefund;
        }
    }

    foreach ($orderPackageList as $orderPackage) {
        if($orderNo == $orderPackage['order_no']) {
            unset($orderPackage['order_no']);
            $orderList[$orderNo]['order_package'][] = $orderPackage;
        }
    }

    foreach ($orderExtendList as $orderExtend) {
        if($orderNo == $orderExtend['order_no']) {
            $orderExtend['store_name'] = mStore::get('abbr', ['id' => $orderExtend['store_id']]);
            unset($orderExtend['order_no']);
            $orderList[$orderNo]['order_extend'][] = $orderExtend;
        }
    }

    foreach ($customizePayWayList as $customizePayWay) {
        if($orderNo == $customizePayWay['order_no']) {
            unset($customizePayWay['order_no']);
            $orderList[$orderNo]['customize_pay_way'][] = $customizePayWay;
        }
    }

    if($order['confirm_time'] == '0000-00-00 00:00:00') {
        $orderList[$orderNo]['confirm_time'] = '0000-01-30 00:00:00';
    }

    if($order['pay_time'] == '0000-00-00 00:00:00') {
        $orderList[$orderNo]['pay_time'] = '0000-01-30 00:00:00';
    }

    if($order['delivery_time'] == '0000-00-00 00:00:00') {
        $orderList[$orderNo]['delivery_time'] = '0000-01-30 00:00:00';
    }

    $result = OrderSearch::saveByOrderId($orderList[$orderNo]);


    if($result['code'] != 1) {
        var_dump($result);
    }

    echo "总数据$sum,已经同步".$i++;
}

    $start += 1000;

} while($orderList);


